﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;

namespace OpenApi
{
	/// <summary>
	/// 认证终结点行为
	/// </summary>
	public class AuthEndpointBehavior : IEndpointBehavior
	{
		private IAuthProvider m_Auth;

		public AuthEndpointBehavior(IAuthProvider auth)
		{
			this.m_Auth = auth;
		}

		public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
		{
			//throw new NotImplementedException();
		}

		public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
		{
			//throw new NotImplementedException();
		}

		public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
		{
			// 给终结点的操作上附加认证行为
			endpoint.Contract.Operations.ToList().ForEach(op => op.OperationBehaviors.Add(new AuthOperationBehavior(this.m_Auth)));
		}

		public void Validate(ServiceEndpoint endpoint)
		{
			//throw new NotImplementedException();
		}
	}
}
